package com.tanhua.dubbo.server.api.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.pojo.UserLike;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
//@Component
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private UserLikeApi userLikeApi;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {

        Query query = Query.query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        return this.mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long toUserId, Integer pageNum, Integer pageSize) {
        //构造分页以及排序的参数
        //第一页页数从0开始，所以页数pageNum需要-1
        PageRequest pageRequest = PageRequest.of(pageNum -1,pageSize, Sort.by(Sort.Order.desc("score")));
        //构造查询的参数
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)).with(pageRequest);
        List<RecommendUser> recommendUserList = this.mongoTemplate.find(query, RecommendUser.class);
        return new PageInfo<>(0,pageNum,pageSize,recommendUserList);
    }

    @Override
    public Double queryScore(Long userId, Long toUserId) {

        // 1-30 30-1
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("toUserId").is(toUserId));
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (ObjectUtil.isEmpty(recommendUser)){
            return null;
        }

        return recommendUser.getScore();
    }

    /**
     * 查询探花列表，查询时需要排除喜欢或者不喜欢列表用户
     *
     * @param userId
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryCardList(Long userId, Integer count) {
        PageRequest pageRequest = PageRequest.of(0,count,Sort.by(Sort.Order.desc("score")));

        //排除已喜欢或不喜欢的用户
        List<Long> userIdList = new ArrayList<>();
        //查询喜欢用户
        List<Long> likeList = this.userLikeApi.queryLikeList(userId);
        userIdList.addAll(likeList);
        //查询不信换用户
        List<Long> notLikeList = this.userLikeApi.queryNotLikeList(userId);
        userIdList.addAll(notLikeList);
        //构造查询条件
        Criteria criteria = Criteria.where("userId").is(userId);
        if (CollUtil.isNotEmpty(userIdList)){
            criteria.andOperator(Criteria.where("userId").nin(userIdList));
        }

        Query query = Query.query(criteria).with(pageRequest);
        List<RecommendUser> recommendUserList = this.mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }
}
